﻿using Hotel.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.DAL
{
    public class ShopDAL
    {
        public ShopDAL(HotelConText hotelConText)
        {
            HotelConText = hotelConText;
        }

        public HotelConText HotelConText { get; }



        /// <summary>
        /// 添加门店
        /// </summary>
        /// <param name="shopModel"></param>
        /// <returns></returns>
        public int ShopAdd(ShopModel shopModel)
        {
            HotelConText.shops.Add(shopModel);
            return HotelConText.SaveChanges();
        }




        /// <summary>
        /// 门店不能重复
        /// </summary>
        /// <param name="shopName"></param>
        /// <returns></returns>

        public int SearchShopName(string shopName)
        {
            int res = HotelConText.shops.Where(x => x.ShopName == shopName).Count();
            return res;
        }


        /// <summary>
        ///  门店列表
        /// </summary>
        /// <param name="count">总条数</param>
        /// <param name="page">总页码</param>
        /// <param name="rightName">名称</param>
        /// <param name="createtiem">时间</param>
        /// <param name="index">页码</param>
        /// <param name="size">页容量</param>
        /// <returns></returns>
        public List<ShopModel> GetShopList(out int count, out int page, string? shopName = "", int index = 1, int size = 2)
        {
            List<ShopModel> list = HotelConText.shops.Where(x => x.IsDeleted == false).ToList();
            if (!string.IsNullOrEmpty(shopName))
            {
                list = list.Where(x => x.ShopName.Contains(shopName)).ToList();
            }


            count = list.Count;
            page = (int)Math.Ceiling(count * 1.0 / size);
            //分页
            list = list.OrderBy(x => x.Id).Skip((index - 1) * size).Take(size).ToList();

            return list;

        }






        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DelAllShopItem(string ids)
        {
            var idsArr = ids.Split(',');
            foreach (var id in idsArr) 
            {
                var inofo = HotelConText.shops.Find(Convert.ToInt32(id));
                inofo.IsDeleted = true;
            }

            return HotelConText.SaveChanges();
        }




        /// <summary>
        /// 批量上下架
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int UpOrDownAllShowItem(string ids,int state) 
        {

            var idsArr = ids.Split(',');
            foreach (var id in idsArr)
            {
                var inofo = HotelConText.shops.Find(Convert.ToInt32(id));
                inofo.State = state;
            }

            return HotelConText.SaveChanges();
        }


























    }
}
